/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.main;
import view.actor.*;
import java.awt.event.*;
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import model.game.*;
import java.util.ArrayList;


/**
 *
 * @author Satrio Dewantono
 */
public class UserInterface implements ActionListener{
  private View V;
  private Timer timer;
  private BackGround bg;
  private PlayerActor ps;
  private GameController controller_;
  private InformationLayer il_;
  private GameEngine game_;
  private SpeechBubbleManager sbm_;
  private Actors actors;
  private DayTimer dt;
  private boolean buymode,hiremode;
  private int shopindex,hireindex;
  private ArrayList<String> shoplist;
  private ArrayList<Image> shopimglist;
  private ArrayList<String> hirelist;
  private ArrayList<Image> hireimglist;

  public void actionPerformed(ActionEvent e) {
    bg.move();
    ps.setImage(ps.getImgIdx());
    V.getDrawBoard().repaint();
    if (game_.getCreatures().getPlayer().getCell().isTeleport()){
      sbm_.teleportPossible();
    }else if ((!sbm_.isTimedMessage())&&(!buymode)&&(!hiremode)){
      sbm_.stopMessage();
    }
    if (dt.isNewDay()){
      bg.makeDrawList();
      bg.resetPos();
      actors.findAllActors();
      dt.tickNewDayOK();
    }
    if (dt.tick()){
      actors.tickActors();
      actors.findMovingActors();
      dt.tickOK();
    }
    actors.moveActors(bg.getDX(),bg.getDY());
    //game_.advanceTimeDay();
  }

  public UserInterface(GameEngine eng){
    game_ = eng;
    il_ = new InformationLayer(eng);
    bg = new BackGround(game_);
    ps = new PlayerActor(game_,bg);
    actors  = new Actors(game_);
    sbm_ = new SpeechBubbleManager(il_);
    V = new View(bg,ps,game_,sbm_,actors);
    V.addKeyListener(new TAdapter());
    V.getDrawBoard().setDoubleBuffered(true);
    dt = new DayTimer(game_,actors);
    buymode = false;
    hiremode = false;

    timer = new Timer(5,this);
    timer.start();
  }

  public void paint(Graphics g) {
    V.getDrawBoard().paint(g);
  }

  private class TAdapter extends KeyAdapter {

    @Override
    public void keyReleased(KeyEvent e) {
      int key = e.getKeyCode();
      bg.keyReleased(e);
      ps.keyReleased(e);

      if (key == KeyEvent.VK_Q){
	sbm_.showSelectedItem();
      }else
      if (key == KeyEvent.VK_E){
	sbm_.showSelectedItem(); 
      }else
      if (key == KeyEvent.VK_T){
	bg.makeDrawList();
	bg.setAbsX(game_.getCreatures().getPlayer().getCell().getX());
	bg.setAbsY(game_.getCreatures().getPlayer().getCell().getY());
	actors.findAllActors();
      }else

      if (key == KeyEvent.VK_S){
	actors.findAllActors();
	//bg.makeDrawList();
      }else

      if (key == KeyEvent.VK_B){
	if(!buymode){
	  //if(game_.getMaps().getMapContaining(game_.getCreatures().getPlayer().getCell()).isShop()){
	    buymode = true;
	    shopindex = 0;
	    shoplist = il_.getAllShopItemsNames();
	    shopimglist = il_.getAllShopItemsImages();
	    sbm_.buyMode(shopimglist.get(shopindex));
	  //}
	}else{
	  ps.buyStuff(shoplist.get(shopindex));
	  //sbm_.exitBuyMode();
	  buymode = false;
	}
      }else

      if (key == KeyEvent.VK_V){
	if((buymode) && shopindex>0){
	  shopindex--;
	  sbm_.buyMode(shopimglist.get(shopindex));
	}
      }else

      if (key == KeyEvent.VK_N){
	if((buymode) && shopindex<shoplist.size()-1){
	  shopindex++;
	  sbm_.buyMode(shopimglist.get(shopindex));
	}
      }else

      if (key == KeyEvent.VK_W){
	actors.findAllActors();
      }else

      if (key == KeyEvent.VK_H){
	if(!hiremode){
	  //if(game_.getMaps().getMapContaining(game_.getCreatures().getPlayer().getCell()).isShop()){
	    hiremode = true;
	    hireindex = 0;
	    hirelist = il_.getAllSpritesNames();
	    hireimglist = il_.getAllSpritesImage();
	    sbm_.hireMode(hireimglist.get(hireindex));
	  //}
	}else{
	  if (!game_.getCreatures().getSprites().getSpriteFromName(hirelist.get(hireindex)).isHired()){
	    ps.hire(hirelist.get(hireindex));
	    //sbm_.exitBuyMode();
	  }else{
	    ps.fire(hirelist.get(hireindex));
	  }
	  hiremode = false;
	}
      }else

      if (key == KeyEvent.VK_G){
	if((hiremode) && hireindex>0){
	  hireindex--;
	  sbm_.hireMode(hireimglist.get(hireindex));
	}
      }else

      if (key == KeyEvent.VK_J){
	if((hiremode) && hireindex<hirelist.size()-1){
	  hireindex++;
	  sbm_.hireMode(hireimglist.get(hireindex));
	}
      }
    }

    @Override
    public void keyPressed(KeyEvent e) {
      bg.keyPressed(e);
      ps.keyPressed(e);
    }

    @Override
    public void keyTyped(KeyEvent e) {
      ps.keyTyped(e);
    }
  }

}
